//    Copyright (C) Kherty.  All rights reserved.
using System.Windows;
using System.Windows.Media;
using OpenLS.Core.Serialization;

namespace OpenLS.Drawing
{
    /// <summary>
    /// Specifies that the drawing should tile its container
    /// </summary>
    public class TileModeProperties : FillModeProperties
    {
        private RectAlignment? alignment;
        private TileFlipMode flip;
        private int? sx;
        private int? sy;
        private int? tx;

        private int? ty;

        public int? Tx
        {
            get { return tx; }
            set { tx = value; }
        }

        public int? Ty
        {
            get { return ty; }
            set { ty = value; }
        }

        public int? Sx
        {
            get { return sx; }
            set { sx = value; }
        }

        public int? Sy
        {
            get { return sy; }
            set { sy = value; }
        }

        public TileFlipMode Flip
        {
            get { return flip; }
            set { flip = value; }
        }

        public RectAlignment? Alignment
        {
            get { return alignment; }
            set { alignment = value; }
        }

        internal override void ReadXmlCore(ReadContext context)
        {
            using (var c = context.Read("tile"))
            {
                Tx = c.GetOptionalInteger("tx");
                Ty = c.GetOptionalInteger("ty");
                Sx = c.GetOptionalInteger("sx");
                Sy = c.GetOptionalInteger("sy");
                Flip = c.GetOptionalEnum("flip", TileFlipMode.None);
                Alignment = c.GetOptionalEnum<RectAlignment>("algn");
            }
        }

        internal override void WriteXmlCore(WriteContext context)
        {
            using (WriteContext c = context.Write("tile"))
            {
                c.SetOptionalInteger("tx", Tx);
                c.SetOptionalInteger("ty", Ty);
                c.SetOptionalInteger("sx", Sx);
                c.SetOptionalInteger("sy", Sy);
                c.SetOptionalEnum<TileFlipMode>("flip", Flip);
                c.SetOptionalEnum("algn", Alignment);
            }
        }


        public override void Apply(ImageBrush brush)
        {
            switch (Flip)
            {
                case TileFlipMode.None:
                    brush.TileMode = TileMode.Tile;
                    break;
                case TileFlipMode.X:
                    brush.TileMode = TileMode.FlipX;
                    break;
                case TileFlipMode.Y:
                    brush.TileMode = TileMode.FlipY;
                    break;
                case TileFlipMode.XY:
                    brush.TileMode = TileMode.FlipXY;
                    break;
            }
            brush.Viewport = new Rect(new Point(0, 0), new Size(brush.ImageSource.Width, brush.ImageSource.Height));
            brush.ViewportUnits = BrushMappingMode.Absolute;
            //\\not finished
        }
    }
}